﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.API.Application.Command.MenuCommand;
using RBAC.MaHaiBo.API.Application.Command.UserCommand;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service;
using System.Security.Claims;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 菜单 控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
   [Authorize]
    public class MenuController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<MenuController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mMediator;
        IMenuService mser;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mMediator">中介者</param>
        public MenuController(ILogger<MenuController> logger, IMediator mMediator ,IMenuService mser)
        {
            this.logger = logger;
            this.mMediator = mMediator;
            this.mser = mser;
        }



        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu">要添加的数据</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMenu(MenuCreateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;

                logger.LogError("【菜单 控制器】 添加菜单 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 查看省份有子菜单
        /// </summary>
        /// <param name="Mid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<int>> MenuCount([FromQuery]MenuCountCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data= await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单服务层】 查看省份有子菜单 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="request">要删除的菜单编号</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> DeleteMenu([FromQuery]MenuDeleteCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单 控制器】 删除菜单信息 出错" + ex.Message);
                throw;
            }
            return result;
        }



        /// <summary>
        /// 显示全部菜单列表
        /// </summary>
        /// <returns>返回数据列表集合</returns>
        [HttpGet]
        public async Task<ApiResult<FenYeDTO<MenuDTO>>> GetAllMenuList([FromQuery]MenuShowCommand request)
        {
            ApiResult<FenYeDTO<MenuDTO>> result = new ApiResult<FenYeDTO<MenuDTO>>();
            try
            {
                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单 控制器】 显示全部菜单列表 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 树形菜单列表
        /// </summary>
        /// <returns>返回数据列表集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<Tmeen>>> GetAllTmeenMenuList([FromQuery] MenuShow1Command request)
        {
            ApiResult<List<Tmeen>> result = new ApiResult<List<Tmeen>>();
            try
            {
                request.Uid =Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单 控制器】 树形菜单列表 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示父级列表
        /// </summary>
        /// <returns>返回 显示父级列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<Menu>>> GetMenuList()
        {
            ApiResult<List<Menu>> result = new ApiResult<List<Menu>>();
            try
            {
                result.Data= await mMediator.Send(new UserShowPidCommand());
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单 控制器】 显示全部菜单列表 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填菜单信息
        /// </summary>
        /// <param name="request">菜单编号</param>
        /// <returns>返回菜单数据信息</returns>
        [HttpGet]
        public async Task<ApiResult<Menu>> GetMenuInfo([FromQuery]MenuBackfillCommand request)
        {
            ApiResult<Menu> result = new ApiResult<Menu>();
            try
            {
                result.Data = await mMediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单服务层】 反填菜单信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="request">修改后的数据信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateMenu(MenuupdateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code=await mMediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -2;
                result.Message = ex.Message;
                logger.LogError("【菜单 控制器】 修改菜单信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        
    }
}
